package org.forex.impl.engine;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

import org.forex.impl.ws.DeleteOrder;
import org.forex.impl.ws.GetInstrumentPrice;
import org.forex.impl.ws.GetTrades;
import org.forex.impl.ws.MakeOrder;
import org.forex.vo.Instrument;
import org.forex.vo.Order;
import org.forex.vo.Prices;

public class CheckOrder implements Runnable {

	private Instrument in;
	private String operation;
	//private final Float margine=new Float(0.001);
	private Long units=new Long(5l);
//	private int andamento=0;
//	
//	private List<Double> positivi=new LinkedList<Double>();
//	private List<Double> negativi=new LinkedList<Double>();
//	
//	private Instrument prev;
	private Order order;
	private List<Instrument> priceList;
	private double prevProfitto=-10000;
	
	public CheckOrder(Instrument in) {
		super();
		this.setIn(in);
		priceList=new LinkedList<Instrument>();
	}

	@Override
	public void run() {
		//System.out.printf("Check Order for: %s\n",in.getInstrument());
//		try {
//			Prices prices=new GetInstrumentPrice().get(getIn().getInstrument());
//			
//			Optional<Instrument> opIn=prices.getInstruments().stream().filter(p->p.getInstrument().equalsIgnoreCase(in.getInstrument())).findFirst();
//			
//			if(opIn.isPresent()){
//				if(priceList.size()>12)
//					priceList.remove(0);
//				priceList.add(opIn.get());
//			
//			
//				operation=null;
//				
//				if(priceList.size()>11)
//					operation=new Andamento().check(priceList);
//				
//				if(order!=null){
//					double profitto=0.0;
//					switch(order.getTradeOpened().getSide()){
//					case Order.BUY:
//						profitto=opIn.get().getBid().doubleValue()-order.getPrice().doubleValue();						
//						break;
//					case Order.SELL:
//						profitto=order.getPrice().doubleValue()-opIn.get().getBid().doubleValue();
//						break;
//					}
//									
//					System.out.printf("\tProfitto for %s %f\n",in.getInstrument(),profitto);
//					
//					if(profitto < -0.05){
//						deleteOrder();
//					}
//					
//					if(prevProfitto==-10000)
//						prevProfitto=profitto;
//					else{
//						if((prevProfitto > profitto) && prevProfitto-profitto>0.0001)
//							deleteOrder();
//						prevProfitto=profitto;
//					}
//				}
//				
//				if(operation!=null){
//					if(order==null){
//						System.out.printf("\tMake %s new Order for: %s\n",operation,in.getInstrument());
//						order=new MakeOrder().post(in.getInstrument(), units, operation);
//					}else{
//						if(order.getTradeOpened().getSide()!=null && !order.getTradeOpened().getSide().equalsIgnoreCase(operation)){
//							deleteOrder();
//						}
//					}
//				}
//			}
			
			
//			if(prev==null && opIn.isPresent()){
//				System.out.printf("\tSet first value for: %s\n",in.getInstrument());
//				prev=opIn.get();
//				return;
//			}
//			
//			if(opIn.isPresent()){
//				
//				System.out.printf("\tTest bid for: %s\n",in.getInstrument());
//				
//				double prevPrice=prev.getBid().doubleValue();
//				double currPrice=opIn.get().getBid().doubleValue();
//				
//				System.out.printf("\tPrev: %f\n",prevPrice);
//				System.out.printf("\tCurrent: %f\n",currPrice);
//								
//				double diff=prevPrice - currPrice;
//				
//				if(diff>0){
//					//il prezzo sta calando
//					--andamento;
//					negativi.add(currPrice);
//				}
//				
//				if(diff<0){
//					//il prezzo sta crescendo
//					++andamento;
//					positivi.add(currPrice);
//				}
//				
//				System.out.printf("\tPrice Diff: %f\n",diff);
//								
//				checkOrder(diff);						
//
//				prev=opIn.get();
//			}
			
			
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			System.err.println(e.getMessage());
//		}
	}

	/*
	private void checkOrder(double diff) throws IOException {
		
		boolean makeOrder=false;
		
		if(andamento>0 && positivi.size()>4 && positivi.size()>negativi.size()){
			//andamento positivo
			operation=Order.BUY;
			makeOrder=true;
		}
		
		if(andamento<0 && negativi.size()>4 && negativi.size()>positivi.size()){
			//andamento negativo
			operation=Order.SELL;
			makeOrder=true;
		}
		
		if(makeOrder){		
			if(order==null){
				System.out.printf("\tMake new Order for: %s\n",in.getInstrument());
				order=new MakeOrder().post(in.getInstrument(), units, operation);
			}else{
				if(order.getTradeOpened().getSide()==null)
					new GetTrades().
				
				if(order.getTradeOpened().getSide()!=null && !order.getTradeOpened().getSide().equalsIgnoreCase(operation)){
					System.out.printf("\tDelete new Order for: %s\n",in.getInstrument());
					order.setId(order.getTradeOpened().getId());
					new DeleteOrder().delete(order);
				}
				order=null;
			}
			
			positivi.clear();
			negativi.clear();
			andamento=0;
		}
	}
	*/

	private void deleteOrder() throws IOException {
		System.out.printf("\tDelete order for: %s\n",in.getInstrument());
		order.setId(order.getTradeOpened().getId());
		new DeleteOrder().delete(order);
		order=null;
		prevProfitto=-10000;
	}

	public Instrument getIn() {
		return in;
	}

	public void setIn(Instrument in) {
		this.in = in;
	}

}
